<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JHtml::_('bootstrap.tooltip');
JHtml::_('behavior.multiselect');
JHtml::_('dropdown.init');
JHtml::_('formbehavior.chosen', 'select');
JHtml::_('behavior.modal', 'a.tables-modal-review');

$user		= JFactory::getUser();
$listOrder	= $this->escape($this->state->get('list.ordering'));
$listDirn	= $this->escape($this->state->get('list.direction'));
$saveOrder	= $listOrder == 'at.ordering';
if ($saveOrder)
{
	$saveOrderingUrl = 'index.php?option=com_autogenerator&task=tables.saveOrderAjax';
	JHtml::_('sortablelist.sortable', 'tablesList', 'adminForm', strtolower($listDirn), $saveOrderingUrl);
}
?>
<link rel="stylesheet" href="<?php echo JUri::root() . AUTOGENERATOR_URL_CSS . 'autogenerator.css'; ?>" type="text/css">
<script src="<?php echo JUri::root() . AUTOGENERATOR_URL_JS . 'autogenerator.js'; ?>" type="text/javascript"></script>


<script type="text/javascript">
    jQuery(document).ready(function() {
        Autogenerator.Tables.importFields();
        Autogenerator.Edit_Inline.init();
        Autogenerator.Change_Status.init();
    });
    
    Joomla.orderTable = function() {
        table = document.getElementById("sortTable");
        direction = document.getElementById("directionTable");
        order = table.options[table.selectedIndex].value;
        if (order != '<?php echo $listOrder; ?>') {
            dirn = 'asc';
        } else {
            dirn = direction.options[direction.selectedIndex].value;
        }
        Joomla.tableOrdering(order, dirn, '');
    }
</script>

<form action="<?php echo JRoute::_('index.php?option=com_autogenerator&view=tables');?>" method="post" name="adminForm" id="adminForm">
    <?php if(!empty( $this->sidebar)): ?>
        <div id="j-sidebar-container" class="span2">
            <?php echo $this->sidebar; ?>
        </div>

        <div id="j-main-container" class="span10">
    <?php else : ?>
        <div id="j-main-container">
    <?php endif;?>
		<div id="filter-bar" class="btn-toolbar">
			<div class="filter-search btn-group pull-left">
				<label for="filter_search" class="element-invisible"><?php echo JText::_('COM_AUTOGENERATOR_TABLES_FILTER_SEARCH_DESC');?></label>
				<input type="text" name="filter_search" placeholder="<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FILTER_SEARCH_DESC'); ?>" id="filter_search" value="<?php echo $this->escape($this->state->get('filter.search')); ?>" title="<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FILTER_SEARCH_DESC'); ?>" />
			</div>
			<div class="btn-group pull-left hidden-phone">
				<button class="btn tip hasTooltip" type="submit" title="<?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?>"><i class="icon-search"></i></button>
				<button class="btn tip hasTooltip" type="button" onclick="document.id('filter_search').value='';this.form.submit();" title="<?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?>"><i class="icon-remove"></i></button>
			</div>
			<div class="btn-group pull-right hidden-phone">
				<label for="limit" class="element-invisible"><?php echo JText::_('JFIELD_PLG_SEARCH_SEARCHLIMIT_DESC');?></label>
				<?php echo $this->pagination->getLimitBox(); ?>
			</div>
			<div class="btn-group pull-right hidden-phone">
				<label for="directionTable" class="element-invisible"><?php echo JText::_('JFIELD_ORDERING_DESC');?></label>
				<select name="directionTable" id="directionTable" class="input-medium" onchange="Joomla.orderTable()">
					<option value=""><?php echo JText::_('JFIELD_ORDERING_DESC');?></option>
					<option value="asc" <?php if ($listDirn == 'asc') echo 'selected="selected"'; ?>><?php echo JText::_('JGLOBAL_ORDER_ASCENDING');?></option>
					<option value="desc" <?php if ($listDirn == 'desc') echo 'selected="selected"'; ?>><?php echo JText::_('JGLOBAL_ORDER_DESCENDING');?></option>
				</select>
			</div>
			<div class="btn-group pull-right">
				<label for="sortTable" class="element-invisible"><?php echo JText::_('JGLOBAL_SORT_BY');?></label>
				<select name="sortTable" id="sortTable" class="input-medium" onchange="Joomla.orderTable()">
					<option value=""><?php echo JText::_('JGLOBAL_SORT_BY');?></option>
					<?php echo JHtml::_('select.options', $this->getSortFields(), 'value', 'text', $listOrder);?>
				</select>
			</div>
		</div>
		<div class="clearfix"> </div>

		<table class="table table-striped" id="tablesList">
			<thead>
				<tr>
					<th width="1%" class="nowrap center hidden-phone">
						<?php echo JHtml::_('grid.sort', '<i class="icon-menu-2"></i>', 'at.ordering', $listDirn, $listOrder, null, 'asc', 'JGRID_HEADING_ORDERING'); ?>
					</th>
					<th width="1%" class="hidden-phone">
						<input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
					</th>
					<!-- START LIST OF HEADER -->
					<th>
						<?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_TABLES_HEADER_COMPONENT', 'ac.title', $listDirn, $listOrder); ?>
					</th>
					<th width="25%" >
						<?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_TABLES_HEADER_SQL_TABLE_NAME', 'at.sql_table_name', $listDirn, $listOrder); ?>
					</th>
					<th width="25%" >
						<?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_TABLES_HEADER_TITLE', 'at.title', $listDirn, $listOrder); ?>
					</th>
					<th width="10%" >
						<?php echo JHtml::_('grid.sort', 'COM_AUTOGENERATOR_TABLES_HEADER_STATUS', 'at.status', $listDirn, $listOrder); ?>
					</th>
                                        <th width="20%" class="center">
						<?php echo JText::_('COM_AUTOGENERATOR_TABLES_HEADER_FUNCTION'); ?>
					</th>
					<!-- END LIST OF HEADER -->
					
					<th width="1%" class="nowrap hidden-phone">
						<?php echo JHtml::_('grid.sort', 'JGRID_HEADING_ID', 'at.id', $listDirn, $listOrder); ?>
					</th>
				</tr>
			</thead>
			<tbody>
			<?php if ($this->items !== false) : ?>
				<?php foreach($this->items as $i => $item) :
					$ordering = ($listOrder == 'ac.ordering');
					?>
				<tr class="row<?php echo $i % 2; ?>" sortable-group-id="<?php echo $item->component_id; ?>">
					<td class="order nowrap center hidden-phone">
						<span class="sortable-handler hasTooltip">
							<i class="icon-menu"></i>
						</span>
						<input type="text" style="display:none" name="order[]" size="5" value="<?php echo $item->ordering; ?>" class="width-20 text-area-order " />
					</td>
					
					<td class="center hidden-phone">
						<?php echo JHtml::_('grid.id', $i, $item->id); ?>
					</td>
                    <td class="small hidden-phone">
						<?php echo $this->escape($item->component); ?>
					</td>
                    
					<td class="nowrap has-context">
						<div class="pull-left">
							<a href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=table.edit&id=' . $item->id);?>" title="<?php echo JText::_('JACTION_EDIT');?>">
								<?php echo $this->escape($item->sql_table_name); ?>
							</a>
						</div>
						<div class="pull-left">
							<?php
								// Create dropdown items
								JHtml::_('dropdown.edit', $item->id, 'table.');
								// Render dropdown list
								echo JHtml::_('dropdown.render');
								?>
						</div>
					</td>
                    <td class="small hidden-phone autogenerator-edit-inline-area">
						<span class="autogenerator-edit-inline" title="<?php echo JText::_('COM_AUTOGENERATOR_XML_ELEMENTS_XML_TYPE_CUSTOM_CLICK_TO_EDIT_INLINE'); ?>"><?php echo $this->escape($item->title); ?></span>
						
						<span class="autogenerator-holder" style="display: none;">
							<input type="text" class="autogenerator-custom" name="holder[<?php echo (int) $item->id; ?>]" value="" />
							
							<input class="autogenerator-id" type="hidden" name="holder[id]" value="<?php echo (int) $item->id; ?>" />
							&nbsp;&nbsp;
							<a href="#" class="autogenerator-save">Save</a>
							&nbsp;&nbsp;
							<a href="#" class="autogenerator-cancel">Cancel</a>
						</span>
						
					</td>
					<td class="small hidden-phone">
						<a class="btn btn-micro autogenerator-change-status" href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=table.changeStatus&iTableId=' . $item->id);?>" title="<?php echo ($item->status ? 'Enable' : 'Disable'); ?>">
							<i class="<?php echo ($item->status ? 'icon-publish' : 'icon-unpublish'); ?>"></i>
						</a>
					</td>
                    <td class="nowrap has-context">
						<div class="center">
							<a class="tables-modal-review" href="<?php echo JRoute::_('index.php?option=com_autogenerator&view=tables&layout=modal&tmpl=component&iTableId=' . $item->id);?>" rel="{handler: 'iframe', size: {x: 800, y: 500}}" title="<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FUNCTION_REVIEW_LABEL');?>">
								<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FUNCTION_REVIEW_LABEL'); ?>
							</a>
                            &nbsp;|&nbsp;
                            <a href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=table.exportPHPFile&iTableId=' . $item->id);?>" title="<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FUNCTION_EXPORT_LABEL');?>">
								<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FUNCTION_EXPORT_LABEL'); ?>
							</a>
                            &nbsp;|&nbsp;
                            <a class="tables-import-fields" href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=table.importFields&iTableId=' . $item->id);?>" title="<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FUNCTION_IMPORT_LABEL');?>">
								<?php echo JText::_('COM_AUTOGENERATOR_TABLES_FUNCTION_IMPORT_LABEL'); ?>
							</a>
						</div>
					</td>
					<td class="center hidden-phone">
						<?php echo (int) $item->id; ?>
					</td>
				</tr>
				<?php endforeach; ?>
				<?php endif; ?>
			</tbody>
		</table>
		<?php echo $this->pagination->getListFooter(); ?>
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
		<?php echo JHtml::_('form.token'); ?>
		
                <a class="autogenerator-link" style="display: none;" href="<?php echo JRoute::_('index.php?option=com_autogenerator&task=table.editInline'); ?>">&nbsp;</a>
	</div>
</form>
